package com.googlecode.groovy_toy_orm

import com.googlecode.groovy_toy_orm.internal.CodeBased
import com.googlecode.groovy_toy_orm.internal.CodeBasedUtil
import org.testng.annotations.Test

/**
 * @author: DavydovMD
 * Date: 16.04.13
 * Time: 19:05
 */
class CodeBasedEnumsTest {

    static enum SillyBool implements CodeBased {
        FALSE(0), TRUE(1)
        final int code

        /**
         * accepts one parameter (int code)
         */
        public static Closure<SillyBool> fromCode = CodeBasedUtil.fromCodeClosure(SillyBool.class)

        SillyBool(code) {
            this.code = code
        }
    }


    @Test
    public void test() {
        assert SillyBool.fromCode(0) == SillyBool.FALSE
        assert SillyBool.fromCode(1) == SillyBool.TRUE
        assert SillyBool.class.invokeMethod("fromCode",1) == SillyBool.TRUE

        SillyBool.fromCode(1)
        UnsupportedOperationException caught = null
        try {
            assert SillyBool.fromCode(3)
        } catch (UnsupportedOperationException e) {
            caught = e
        }
        assert caught instanceof UnsupportedOperationException
    }


}
